<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script src="./js/earthsdk.js"></script>
    <!-- 面板的样式放到css文件内 -->
    <link rel="stylesheet" href="././css/example1.css">
    <title>Cesium地球初始化</title>
    <style>
        html,
        body,
        #app {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
        }


        .box {
            display: grid;
            grid-template-columns: 90px 1fr;
            align-items: center;
        }

        .box label {
            padding-right: 10px;
        }

        .btn {
            padding-top: 10px;

        }

        .btn button {
            width: 286px;

        }

        label {
            line-height: 30px;
        }
    </style>
</head>

<body>
    <div id="app"></div>
    <div id="panel">
        <div class="box">
            <label for="">基准面高度</label> <input id="planeHeight" type="number">
            <label for="">采样间距</label> <input id="planeDistance" type="number" value="1">
            <label for="">挖方体积</label>
            <p id="cutVolume"></p>
            <label for="">填方体积</label>
            <p id="fillVolume"></p>
            <label for="">分析进度</label>
            <p id="progress"></p>
        </div>
        <div class="btn">
            <button onclick="resetPlane()">重新绘制基准面</button>
            <button onclick="editPlane()">编辑当前基准面</button>
            <button onclick="startAnalyse()">开始分析</button>
        </div>


    </div>

    <script>
        /** ---------------------------------地球创建------------------------------------**/

        // Vue中引入方式：import { ESObjectsManager } from 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main';

        const { ESObjectsManager } = XE2['esobjs-xe2-plugin-main']
        const objm = new ESObjectsManager();
        window.g_objm = objm;

        // 创建Ceisum视口
        const viewer = objm.createCesiumViewer({
            domid: document.getElementById("app"),
        });
        viewer.ionAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyZmQzMDI1OC1kMWYyLTQ1ZjEtYTJmNS0yMjY1ZDcxZjEyOTkiLCJpZCI6NjQ3MTgsImlhdCI6MTYyOTQzNDM5M30.m8vkzG05QiAfe6JQ0XPK8z_6KuUVMf_CoSY-YlMnAIg"
        // 监听视口状态
        viewer.statusChanged.don(status => {
            if (status == 'Created') {
                // 视口创建完成，飞到初始视角位置
                objm.activeViewer.flyIn([108.96261440183773, 34.217165976421526, 640.8978652592571], [301.5081035251009, -31.461909655027977, 0.0012513438258845236], 3)

            }
        })

        // 通过json创建一个影像图层
        const imageryLayer = objm.createSceneObjectFromJson({
            "id": "9812a65f-0de0-4f7b-b234-809c0c2fb8ef",
            "type": "ESImageryLayer",
            "maximumLevel": 18,
            "url": "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
        });
        //添加3DTileset用于测量
        const ES3DTileset = objm.createSceneObjectFromJson({
            "id": "c2c8311e-6e50-47a2-b764-1c82f2d0e256",
            "type": "ES3DTileset",
            "name": "ES3DTileset",
            "allowPicking": true,
            "url": "https://www.pipeser.com/cesiumlab/G7qC5vBk/tileset.json"
        })


        /**-------------------------------------体积测量-------------------------------**/
        /**
         * 体积测量，通过此功能可以通过某一基准面对建筑填挖方体积进行估算。
        */
        const { ESVolumeMeasurement } = XE2['esobjs-xe2-plugin-main']
        const esVolumMeasurement = objm.createSceneObject(ESVolumeMeasurement)

        // 监听基准面设置到UI上
        esVolumMeasurement.planeHeightChanged.don((val) => {
            if (val) document.getElementById("planeHeight").value = val.toFixed(2)
        })

        // 监听挖方体积
        esVolumMeasurement.cutVolumeChanged.don((val) => {
            document.getElementById("cutVolume").innerHTML = val.toFixed(2) + '（立方米）'
        })

        // 监听填方体积
        esVolumMeasurement.fillVolumeChanged.don((val) => {
            document.getElementById("fillVolume").innerHTML = val.toFixed(2) + '（立方米）'
        })

        // 监听进度
        esVolumMeasurement.progressChanged.don((val) => {
            document.getElementById("progress").innerHTML = val.toFixed(2) + "%"
        })

        // 基准面
        esVolumMeasurement.planeHeight = 429

        // 点位数组
        esVolumMeasurement.points = [
            [
                108.95903791081015,
                34.219024065077,
                429
            ],
            [
                108.95970733699832,
                34.218978056915056,
                421.1218945304369
            ],
            [
                108.95970537219341,
                34.219488842196135,
                425.11765753369644
            ],
            [
                108.95904554596402,
                34.21954531707457,
                425.4244056818258
            ]
        ]



        // 是否开启深度检测
        esVolumMeasurement.depthTest = true

        // 重新绘制基准面
        function resetPlane() {
            // 执行此方法可以清空体积测量分析结果。
            esVolumMeasurement.clear()
            esVolumMeasurement.editing = false
            esVolumMeasurement.points = []
            esVolumMeasurement.editing = true

        }

        // 编辑当前基准面
        function editPlane() {
            esVolumMeasurement.editing = true
        }

        // 开始分析
        function startAnalyse() {
            // 执行此方法可以清空体积测量分析结果。
            esVolumMeasurement.clear()
            esVolumMeasurement.planeDistance = Number(document.getElementById("planeDistance").value)
            esVolumMeasurement.planeHeight = Number(document.getElementById("planeHeight").value)
            // 执行此方法可以开启体积测量功能。
            esVolumMeasurement.start()

        }


        setTimeout(() => {
            // 开启分析功能
            esVolumMeasurement.start()
        }, 5000)



    </script>
    <!-- 切换UE和Cesium引擎的组件（Vue） -->
    <div id="root">
        <switch-engine :objm="objm"></switch-engine>
    </div>
    <!-- Vue文件 -->
    <script src="./js/vue.global.js"></script>
    <!-- 切换UE和Cesium的组件 -->
    <script src="./js/switchEngineCom.js"></script>
</body>

</html>